//
//  LBWVisitorView.swift
//  weibo
//
//  Created by Limonwei on 15/12/5.
//  Copyright © 2015年 heima. All rights reserved.
//

//登陆视图

import UIKit
//第三方框架实现约束
import SnapKit

class LBWVisitorView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //供外界调用,只有消息,发现,我 这三个界面才隐藏圆圈
    func setVisitorInto(imgName: String?, message: String?){
        
        if let img = imgName, msg = message {
            //隐藏圆圈
            circleView.hidden = true
            
            iconView.image = UIImage(named: img)
            
            messageLabel.text = msg
            
        }else{
            //开始旋转动画
            startAnim()
        }
   
    }
    
    private func startAnim() {
        
        //初始化动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        //旋转
        anim.toValue = 2 * M_PI
        
        //执行时间
        anim.duration = 20
        
        //重复次数
        anim.repeatCount = MAXFLOAT
     
        //解决切换界面停止旋转的问题
        anim.removedOnCompletion = false
        
        //开始动画
        circleView.layer.addAnimation(anim, forKey: nil)
    }

    
    //设置UI视图
    private func setupUI(){
        //设置背景颜色
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        //添加视图控件 ,按顺序添加,有层次
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //第三方添加约束
        //1.给图标添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        //2.给圆圈添加约束
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        //3.给label添加约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.centerX.equalTo(circleView)
            make.width.equalTo(224)
        }
        //4.给注册按钮添加约束
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        //5.登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        //6.阴影视图
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(loginButton.snp_bottom)
            
        }
        
}
    
    
    //懒加载视图控件
    //加载图标
    private lazy var iconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        let imageView = UIImageView(image: image)
        
        return imageView
    }()
    
    //加载圆圈
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //加载显示消息的label
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人，回这里看看有什么惊喜"
        
        label.textAlignment = .Center
        
        label.numberOfLines = 0
        
        return label
        
    }()
    
    //加载注册按钮  不需要私有 可以外部访问
    lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("注册", forState: .Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        
        return button
        
    }()
    
    
    //加载登录按钮  不需要私有 可以外部访问
    lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("登录", forState: .Normal)
        
        button.setTitleColor(UIColor.blackColor(), forState: .Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        
        return button
        
    }()
    
    //加载阴影视图
    private lazy var maskIconView: UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        let imageView = UIImageView(image: image)
        
        return imageView
    }()
    
    
    
    
    func demo(){
        
        //用代码给视图控件添加约束
        //1.给iconView添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        //以视图的中心点水平垂直居中
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //2.给circleView添加约束
        circleView.translatesAutoresizingMaskIntoConstraints = false
        
        //以图标的中心为中心
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //3.给messageLabel添加约束
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        //约束宽高
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        
        
        //4.给注册按钮添加约束
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        // 约束宽度以及高度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        
        //5.给登录按钮添加约束
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        // 约束宽度以及高度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        
        //6.给阴影视图添加约束 VFL方法
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: ["maskIconView": maskIconView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskIconView]-(offset)-[registerButton]", options: [], metrics: ["offset": -35], views: ["maskIconView": maskIconView,"registerButton":registerButton]))

    }


}





















